import {findNameById, getPageConfigure, savePageConfigure, getProject,getColumnContext,getColumnConfig} from '../../services/configColum/index'
import {openNotice, isNull, User} from '../../utils'
import pathToRegexp from 'path-to-regexp'

export default {
  namespace: 'configColum',
  state: {
    lookMuens:{}, //菜单基本信息
    firstDetail: null, // 首页列表编辑信息
    muenId:'', //菜单ID
    detailLoading: true,
    projectList:[], // 栏目项目
    columnList:[], // 栏目内容
    fundList:
      [{ //
        "text": "string",
        "value": "string"
      },
      {
        "text": "date",
        "value": "date"
      },
      {
        "text": "select",
        "value": "select"
      }],
    filterArr:[
      {
        name:'',
        filed:'',
        type:'',
        defaultValue:'',
        multiple:'',
        url:''
      }
    ], // 筛选数组
    tableHeadArr:[
      {
        displayName:'',
        fieldName:''
      }
    ] // 筛选数组
  },
  subscriptions: {
    setup({dispatch, history}, done) {
      const unlisten = history.listen(function (location) {
        const match = pathToRegexp('/configColum/:id').exec(location.pathname);
        dispatch({type: 'init'});
        if (match) {
          dispatch({ // 获取菜单信息
            type: 'getMuensInfo',
            payload: {id:match[1]}
          });
          dispatch({ // 获取首页配置信息
            type: 'getMuensDetailInfo',
            payload: {id:match[1]}
          });
          dispatch({ // 获取项目
            type: 'getProjectList',
            payload: {id:""}
          });
          // dispatch({ // 获取栏目内容列表
          //   type: 'getColumnList',
          //   payload: {id:match[1]}
          // });
        }
      });
      return unlisten;
    }
  },
  effects: {
    // 初始化
    *init({payload}, {call, put}){
      var payload = {
              lookMuens:{},
              firstDetail:{},
              projectList:[], // 栏目项目
              columnList:[], // 栏目内容
              tableHeadArr:[{displayName:'', fieldName:''}]
           };
      yield put({
        type: 'lookMuens',
        payload: payload
      });
    },
    // 获取菜单信息
    *getMuensInfo({payload}, {call, put}){
      const data = yield call(findNameById,payload);
      if (data.status == 200) {
        yield put({
          type: 'lookMuens',
          payload: {
            lookMuens: data.data
          }
        });
        yield put({
          type: 'setMuensId',
          payload: {
            "muenId" : payload
          }
        });
        yield put({
          type: 'hideDetailLoading'
        })}
    },
    // 获取菜单配置信息
    *getMuensDetailInfo({payload}, {call, put}){
      const data = yield call(getPageConfigure,payload);
      if (data.status == 200) {
        if(data.data) {
          if(data.data.paramFilters.length == 0){
            data.data.paramFilters.push({
                name:'',
                filed:'',
                type:'',
                defaultValue:'',
                multiple:'',
                url:''
            });
            data.data.tableHeads.push({
              displayName:'',
              fieldName:''
            });
          }
          yield put({
            type: 'getMuensDetail',
            payload: {
              firstDetail: data.data || {},
              filterArr : data.data.paramFilters,
              tableHeadArr : data.data.tableHeads
            }
          });
        }
        yield put({
          type: 'hideDetailLoading'
        })}
    },
    // 保存菜单配置信息
    *add({payload}, {call, put}){
      const data = yield call(savePageConfigure,payload);
      if (data.status == 200) {
        openNotice('success', '提示', "保存成功");
      }else {
        openNotice('error', '保存失败', data.msg);
      }
    },
    *managerFilterItem({payload},{select,put}){
      yield put({
        type: 'managerFilters',
        payload
      });
    },
    // 得到项目列表
    *getProjectList({payload}, {call, put}){
      const data = yield call(getProject,payload);
      if (data.status == 200) {
        yield put({
          type: 'setProjectList',
          payload: {
            projectList: data.data
          }
        });
      }
    },
    // 得到栏目内容列表
    *getColumnList({payload}, {call, put}){
      const data = yield call(getColumnContext, payload.id);
      if (data.status == 200) {
        yield put({
          type: 'setColumnContext',
          payload: {
            columnList: data.data
          }
        });
      }
    }
  },
  reducers: {
    lookMuens(state, action){
      return {...state, ...action.payload}
    },
    setMuensId(state, action){
      return {...state, ...action.payload}
    },
    setProjectList(state, action){
      return {...state, ...action.payload}
    },
    setColumnContext(state, action){
      return {...state, ...action.payload}
    },
    setColumnConfig(state, action){
      return {...state, ...action.payload}
    },
    getMuensDetail(state, action){
      return {...state, ...action.payload}
    },
    initData(state, action){
      return {...state, ...action.payload}
    },
    hideDetailLoading(state){
      return {...state, detailLoading: false}
    },
    managerFilters(state,action){
      debugger;
      var filterArr = state.filterArr;
      var tableHeadArr = state.tableHeadArr;
      switch (action.payload.type){
        case 'filterItemAdd' :{
          filterArr.splice(action.payload.idx+1, 0, {name:'', filed:'', type:''});
          break ;
        }
        case 'filterItemDelete' :{
          filterArr.splice(action.payload.idx, 1);
          break ;
        }
        case 'tableItemAdd' :{
          tableHeadArr.splice(action.payload.idx+1,0,{name:'', filed:'', type:''});
          break ;
        }
        case 'tableItemDelete' :{
          tableHeadArr.splice(action.payload.idx, 1);
          break ;
        }
      };
      return {...state, ...filterArr, ...tableHeadArr}
    }
  }
}
